package activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.AlphaAnimation;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import util.Constant;
import util.MySharePerference;

/**
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class MainActivity extends Activity {

    private Handler handler;
    private Runnable runnable;
    private View linear;
    //定位
    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = new AMapLocationClientOption();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 6.0以上高亮状态栏
         */
      /*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }*/
        DemoApplication.addActivity(this);
        initView();
        initLocation();

    }

    /**
     * 初始化定位
     *
     * @since 2.8.0
     * @author hongming.wang
     *
     */
    private void initLocation(){
        //初始化client
        locationClient = new AMapLocationClient(MainActivity.this);
        //设置定位参数
        locationClient.setLocationOption(getDefaultOption());
        // 设置定位监听
        locationClient.setLocationListener(locationListener);
        // 启动定位
        locationClient.startLocation();
    }
    /**
     * 默认的定位参数
     * @since 2.8.0
     * @author hongming.wang
     *
     */
    private AMapLocationClientOption getDefaultOption(){
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        //mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(60000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(true);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        //AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        return mOption;
    }
    /**
     * 定位监听
     */
    AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation loc) {
            if (null != loc) {
                //解析定位结果
                //Toast.makeText(MainActivity.this,"定位成功"+loc.getLongitude()+","+loc.getLatitude()+loc.getAddress(),Toast.LENGTH_LONG).show();
                MySharePerference.addSharePerference(MainActivity.this,"LAT",loc.getLatitude()+"");
                MySharePerference.addSharePerference(MainActivity.this,"LNG",loc.getLongitude()+"");
            } else {
               // Toast.makeText(MainActivity.this,"定位失败,",Toast.LENGTH_LONG).show();
            }
        }
    };

    private void initView(){
        linear = findViewById(R.id.linear);
        linear.setVisibility(View.VISIBLE);
        AlphaAnimation animation = new AlphaAnimation(0.2f, 1.0f);
        animation.setDuration(1500);
        linear.setAnimation(animation);

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                if(DemoApplication.getToken().equals("")){
                    Intent intent = new Intent(MainActivity.this, SonActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("code", Constant.LogonFragment_A);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                }else{
                    Intent intent = new Intent(MainActivity.this, ParentActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        };
        handler.postDelayed(runnable, 1500);
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(runnable);
        //注销定位组件
        if(locationClient!=null) {
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
        super.onDestroy();
    }

}
